package com.yiqi.lottery.feature.lottery.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.app.Activity;
import android.view.animation.*;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qiniu.android.utils.StringUtils;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.DateUtil;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DateTool;
import com.yiqi.lottery.common.widget.RoundImageView;
import com.yiqi.lottery.common.widget.banner.BannerView;
import com.yiqi.lottery.common.widget.banner.holder.HolderCreator;
import com.yiqi.lottery.common.widget.banner.holder.ViewHolder;
import com.yiqi.lottery.common.widget.textview.MarqueeView;
import com.yiqi.lottery.entity.bo.HomeBo;
import com.yiqi.lottery.entity.bo.SelectItem;
import com.yiqi.lottery.feature.basketball.view.BasketballMatchBetActivity;
import com.yiqi.lottery.feature.find.view.GameAnalysisActivity;
import com.yiqi.lottery.feature.football.view.FootballSFCActivity;
import com.yiqi.lottery.feature.home.view.PayActivity;
import com.yiqi.lottery.feature.lottery.data.LotteryData;
import com.yiqi.lottery.feature.lottery.view.*;
import com.yiqi.lottery.feature.find.view.LotteryResultsActivity;
import com.yiqi.lottery.feature.find.view.ScoreLiveActivity;
import com.yiqi.lottery.feature.football.view.FootballMatchBetActivity;
import com.yiqi.lottery.feature.lotto.BettingActivity;
import com.yiqi.lottery.framework.DataServer;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by GEILI on 2018/3/26.
 */

public class HomeAdapter extends BaseMultiItemQuickAdapter<MultipleItem, BaseViewHolder> {
    BannerView mBannerView;
    MarqueeView marqueeView;
    private Activity mActivity;
    ReplaceFragmentLisnetaner mReplaceFragment;
    HomeBo mHomeBo;
    private List<SelectItem> mSelectItems = null;//随机选择的大乐透

    public HomeAdapter(HomeBo homeBo, List<MultipleItem> data, Activity mActivity) {
        super(data);
        this.mActivity = mActivity;
        this.mHomeBo = homeBo;
        addItemType(MultipleItem.ITEM_BANNER, R.layout.home_list_banner);
        addItemType(MultipleItem.ITEM_MSG, R.layout.home_list_msg);
        addItemType(MultipleItem.ITEM_TYPE, R.layout.home_list_type);
        addItemType(MultipleItem.ITEM_LUCKY_NUMBER, R.layout.home_list_lucky);
        addItemType(MultipleItem.ITEM_WINNING_COLUMN, R.layout.home_list_winning);
        addItemType(MultipleItem.ITEM_FOCUS_EVENT, R.layout.home_list_last);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultipleItem item) {
        switch (helper.getItemViewType()) {
            case MultipleItem.ITEM_BANNER:
                mBannerView = helper.getView(R.id.banner);
                setBannerData(mBannerView);
                break;
            case MultipleItem.ITEM_MSG:
                marqueeView = helper.getView(R.id.marqueeView);
                if (mHomeBo != null) {
                    marqueeView.startWithList(mHomeBo.getNotice());
                }

                break;
            case MultipleItem.ITEM_TYPE:
                AM.image().bind(R.drawable.bg_mine_user_center, helper.getView(R.id.iv_lottery_ad));
                helper.getView(R.id.btn_lottery_kaijiang).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.startActivity(new Intent(mActivity, LotteryResultsActivity.class));
                    }
                });
                helper.getView(R.id.btn_lottery_bifen).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.startActivity(new Intent(mActivity, ScoreLiveActivity.class));
                    }
                });
                helper.getView(R.id.btn_lottery_gendan).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.startActivity(new Intent(mActivity, BuyTogetherActivity.class));
                    }
                });
                helper.getView(R.id.btn_lottery_zuqiu).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.startActivity(new Intent(mActivity, FootballMatchBetActivity.class));
                    }
                });
                helper.getView(R.id.btn_lottery_lanqiu).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.startActivity(new Intent(mActivity, BasketballMatchBetActivity.class));
                    }
                });
                helper.getView(R.id.btn_lottery_shenfu).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mActivity, FootballSFCActivity.class);
                        intent.putExtra("current_type", 9);
                        mActivity.startActivity(intent);
                    }
                });
                helper.getView(R.id.btn_lottery_jinqiu).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        T.showShort("此彩种暂未开放！");
                        return;
                    }
                });
                helper.getView(R.id.btn_lottery_renxuan).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mActivity, FootballSFCActivity.class);
                        intent.putExtra("current_type", 10);
                        mActivity.startActivity(intent);
                    }
                });
                helper.getView(R.id.btn_lottery_liudian).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        T.showShort("此彩种暂未开放！");
                        return;
                    }
                });

                break;
            case MultipleItem.ITEM_WINNING_COLUMN:
                AM.image().bindToCircleObject(DataServer.WIN, helper.getView(R.id.iv_lottery_focus_events_win));
                AM.image().bindToCircleObject(DataServer.DEFEAT, helper.getView(R.id.iv_lottery_focus_events_defeat));

                if (mHomeBo != null && mHomeBo.getJiaodian() != null) {
                    String str = mHomeBo.getJiaodian().getSpTypeGg_spf();
                    String[] items = str.split(",");
                    String time = DateTool.getWeek(mHomeBo.getJiaodian().getSaleCloseTime()) + "<font color=\"#D83200\">" + DateTool.TimeStamp2Date2(mHomeBo.getJiaodian().getSaleCloseTime() + "") + "</font>截至";
                    helper.setText(R.id.tv_hn, mHomeBo.getJiaodian().getHn())
                            .setText(R.id.tv_an, mHomeBo.getJiaodian().getAn())
                            .setText(R.id.tv_lottery_focus_events_time, Html.fromHtml(time))
                            .setText(R.id.tv_shen, items[0])
                            .setText(R.id.tv_pin, items[1])
                            .setText(R.id.tv_fu, items[2])

                    ;

                    HomeBo.BannerBean mBannerBean = new HomeBo.BannerBean("焦点赛事", mHomeBo.getJiaodian().getUrl());
                    helper.getView(R.id.btn_lottery_foucs).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(mActivity, BannerWebActivity.class);
                            intent.putExtra("banner", mBannerBean);
                            mActivity.startActivity(intent);
                        }
                    });
                }
                break;

            case MultipleItem.ITEM_LUCKY_NUMBER:
                try {
                    if (mHomeBo != null && mHomeBo.getDaletou() != null) {
                        helper.setText(R.id.tv_lottery_lucky_qi, "第" + mHomeBo.getDaletou().getIssue() + "期\t\t" + DateUtil.getFormatTime(mHomeBo.getDaletou().getStopDate(), "MM-dd HH:mm") + "开奖");
                    }
                } catch (Exception e) {

                }


                RecyclerView rvLucky = helper.getView(R.id.rv_lucky);
                rvLucky.setLayoutManager(new GridLayoutManager(mActivity, 7));
                LuckyNumAdapter luckyNumAdapter = new LuckyNumAdapter();
                luckyNumAdapter.bindToRecyclerView(rvLucky);
                mSelectItems = new ArrayList<>();
                mSelectItems = DataServer.getLuckyRandomData();
                luckyNumAdapter.setNewData(mSelectItems);

                ImageView imageView = helper.getView(R.id.iv_lottery_lucky_change);
                helper.getView(R.id.btn_lottery_lucky_change).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Animation circle_anim = AnimationUtils.loadAnimation(mActivity, R.anim.anim_round_rotate);
                        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
                        circle_anim.setInterpolator(interpolator);
                        circle_anim.setFillAfter(true);
                        if (circle_anim != null) {
                            imageView.startAnimation(circle_anim);  //开始动画
                        }

                        if (luckyNumAdapter != null) {
                            mSelectItems = DataServer.getLuckyRandomData();
                            luckyNumAdapter.setNewData(mSelectItems);
                        }


                    }
                });

                helper.getView(R.id.btn_lottery_lucky_number).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            if (mSelectItems != null) {
                                String[] data = LotteryData.getLottery(mSelectItems, false);

                                Intent intent = new Intent(mActivity, PayActivity.class);
                                intent.putExtra("order_type", 1);
                                intent.putExtra("data", LotteryData.getDLTData(data));
                                intent.putExtra("is_charsing", 2);
                                intent.putExtra("charsing_num", 1);
                                intent.putExtra("issue", mHomeBo != null && mHomeBo.getDaletou() != null ? mHomeBo.getDaletou().getIssue() : "");
                                intent.putExtra("total_amount", 2);
                                mActivity.startActivity(intent);
                                mActivity.overridePendingTransition(R.anim.push_bottom_in, R.anim.push_buttom_out);
                            }
                        } catch (Exception e) {

                        }


                    }
                });
                helper.getView(R.id.btn_lottery_lucky_all).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        mActivity.startActivity(new Intent(mActivity, LuckyNumberActivity.class));

                    }
                });


                break;
            case MultipleItem.ITEM_FOCUS_EVENT:
                helper.getView(R.id.btn_lottery_winning).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mActivity, GameAnalysisActivity.class);
                        intent.putExtra("type",4);
                        mActivity.startActivity(intent);
                    }
                });
                break;

        }
    }

    public void onStart() {
        if (marqueeView != null)
            marqueeView.startFlipping();
    }

    public void onResume() {
        if (mBannerView != null)
            mBannerView.start();
        if (marqueeView != null)
            marqueeView.startFlipping();

    }

    public void onPause() {
        if (mBannerView != null)
            mBannerView.pause();
        if (marqueeView != null)
            marqueeView.stopFlipping();
    }

    public void onStop() {

    }


    /**
     * 设置banner数据
     */
    private void setBannerData(BannerView mBannerView) {
        mBannerView.setIndicatorRes(R.drawable.dot_unselect_image, R.drawable.dot_select_image);
        mBannerView.setIndicatorVisible(true);
        if (mHomeBo != null)
            mBannerView.setPages(mHomeBo.getBanner(), new HolderCreator<BannerViewHolder>() {
                @Override
                public BannerViewHolder createViewHolder() {
                    return new BannerViewHolder();
                }
            });

        mBannerView.start();
    }

    public static class BannerViewHolder implements ViewHolder<HomeBo.BannerBean> {
        private RoundImageView mImageView;

        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            mImageView = view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, int position, HomeBo.BannerBean data) {
            // 数据绑定
            AM.image().bind(data.getFile(), mImageView);

            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, BannerWebActivity.class);
                    intent.putExtra("banner", data);
                    context.startActivity(intent);
                }
            });
        }
    }

    public interface ReplaceFragmentLisnetaner {
        void doReplace();
    }

    public void setReplcae(ReplaceFragmentLisnetaner replaceFragment) {
        this.mReplaceFragment = replaceFragment;
    }

}
